const puppeteer = require('puppeteer');
const devices = puppeteer.devices
const iPhone = devices['iPhone X'];

(async () => {
  const browser = await puppeteer.launch({
    headless: false,
    // args: ['--start-maximized'],
    slowMo: 250,
    devTools: true
  });
  const page = await browser.newPage();
  // await page.emulate(iPhone);
  await page.goto('https://www.qianjifang.cn/news.html');
  await page.type('.search_box input', '桑黄', {
    delay: 100
  });
  await page.click('.search_btn');
  await page.waitFor(2000); // 这个时间内手动登录
  // console.log('222222')

  const target = await browser.waitForTarget(t => t.url().includes('https://www.qianjifang.cn/news.html'));

  const newPage = await target.page();

  let jsonData = [];

  const items = await newPage.$$eval('.news_list li', e => {
    let data = [];
    for(var i=0; i<e.length; i++) {
      let item = e[i];
      let src = item.querySelector('.news_list_href .list_img img').getAttribute('src');
      data[i] = src;
    }
    return data
  });

  console.log(items);

  await browser.close();
})();